<?php

/**
 * shortcut to create url
 *
 * @param mixed $route
 * @param mixed $params
 * @param mixed $ampersand
 */
function url($route, $params = array(), $ampersand = '&')
{
    return Yii::app()->controller->createUrl($route, $params, $ampersand);
}

/**
 * site base url
 */
function baseUrl()
{
    return Yii::app()->request->getBaseUrl(true);
}

/**
 * theme base url
 */
function themeUrl()
{
    if (Yii::app()->theme)
        return Yii::app()->theme->baseUrl;
    else
        return Yii::app()->baseUrl;
}

/**
 * Use for convert number format to right way base on currency.
 * @param float $number
 * @param string $currency
 * @param bool $use_wrap
 * @return float|string
 */
function smoney_format($number=0.0,$currency='',$use_wrap=true){
    $newNumber=$number;
    switch ($currency) {
        case "HKD":
        case "USD":
            //2,334.6USD
            $newNumber=number_format($newNumber,2);
            return $use_wrap?'$'.$newNumber:$newNumber;
        case "VND":
            //10.000 VND
            $newNumber=number_format($newNumber,0,",",".");
            return $use_wrap?$newNumber.' &#8363;':$newNumber;
        default:
            $newNumber=number_format($newNumber,2);
            return $newNumber;
    }

    //return $newNumber;
}

/**
 * Application base path
 */
function basePath()
{
    return Yii::app()->basePath;
}

/**
 * Application runtime path
 */
function runtimePath()
{
    return Yii::app()->basePath . '/runtime';
}

/**
 * Application cache path
 */
function cachePath()
{
    return Yii::app()->basePath . '/runtime/cache';
}

/**
 * Application clientScript object
 * @return CClientScript
 */
function cs()
{
    return Yii::app()->clientScript;
}

/**
 * @return CHttpRequest
 */
function request(){
    return Yii::app()->request;
}
/**
 * current logged in user
 * @return CWebUser
 */
function user()
{
    return Yii::app()->user;
}

/**
 * @return CController
 */
function controller(){
    return Yii::app()->controller;
}

/**
 * Convert unix timestamp to time
 * @param $intTime
 * @return string
 */
function convertIntToTime($intTime)
{
    return intval($intTime / 60) . ":" . str_pad(($intTime % 60), 2, "0", STR_PAD_LEFT);
}

/**
 * @param $imagePath
 * @param $width
 * @param $height
 * @return string
 */
function imageCrop1($imagePath,$width,$height){
    if (trim($imagePath)==''){
        $imagePath='files/no-image.png';
    }
    if (!file_exists($imagePath)) return '';
    return baseUrl() . "/image.php?thumb=" . FlexImage::createThumbFilename($imagePath, intval($width), intval($height));
}

function getQuery($name, $default){
        return Yii::app()->request->getQuery($name, $default);
}
function getState($name, $default){
        return Yii::app()->user->getState($name, $default);
}